package com.edu.policy.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.jetbrains.annotations.NotNull;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.List;

/**
 * 常用工具类
 */
public class CommonUtil {
    /**
     * 补零方法
     */
    public static String generateNum() {
        // 创建一个Random对象
        Random random = new Random();

        // 生成一个6位数的随机数字
        return String.valueOf(random.nextInt(900000) + 100000);
    }
    /** Object -> Class */
    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
    public static List<Long> convertStringListToLongList(List<String> stringList) {
        List<Long> longList = new ArrayList<>();
        for (String str : stringList) {
            // 将字符串转换为Long类型并添加到新的List中
            longList.add(Long.parseLong(str));
        }
        return longList;
    }

    public static byte[] generateQRCode(String text, int width, int height) throws Exception {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 2); // 调整margin值控制内边距

        var bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage bufferedImage = getBufferedImage(width, height, bitMatrix);

        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "PNG", pngOutputStream);
        return pngOutputStream.toByteArray();
    }
    /**
     * 将 BigDecimal 类型的金额（元）转换为 int 类型的金额（分）
     *
     * @param amountInYuan BigDecimal 类型的金额（元）
     * @return int 类型的金额（分）
     */
    public static int convertYuanToFen(BigDecimal amountInYuan) {
        // 乘以 100 并转换为 int 类型
        return amountInYuan.multiply(new BigDecimal(100)).intValue();
    }

    @NotNull
    private static BufferedImage getBufferedImage(int width, int height, BitMatrix bitMatrix) {
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bufferedImage.createGraphics();

        Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        graphics.setColor(Color.BLACK);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
            }
        }
        return bufferedImage;
    }
}
